{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-29T10:25:30.023945Z",
     "start_time": "2025-03-29T10:25:28.423346Z"
    }
   },
   "source": [
    "from sklearn.feature_extraction import DictVectorizer\n",
    "from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer\n",
    "from sklearn.preprocessing import MinMaxScaler, StandardScaler\n",
    "from sklearn.feature_selection import VarianceThreshold\n",
    "from sklearn.decomposition import PCA\n",
    "import jieba\n",
    "import numpy as np\n",
    "from sklearn.impute import SimpleImputer"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:25:30.044949Z",
     "start_time": "2025-03-29T10:25:30.037951Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def mm():\n",
    "    \"\"\"\n",
    "    归一化处理\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    mm = MinMaxScaler(feature_range=(0, 1))\n",
    "\n",
    "    data = mm.fit_transform([[90, 2, 10, 40], [60, 4, 15, 45], [75, 3, 13, 46]])\n",
    "    print(data)\n",
    "    print(\"-\" * 50)\n",
    "    out = mm.transform([[1, 2, 3, 4], [6, 5, 8, 7]])\n",
    "    print(out)\n",
    "    return None\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    mm()"
   ],
   "id": "e135a871f0b73c44",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.         0.         0.         0.        ]\n",
      " [0.         1.         1.         0.83333333]\n",
      " [0.5        0.5        0.6        1.        ]]\n",
      "--------------------------------------------------\n",
      "[[-1.96666667  0.         -1.4        -6.        ]\n",
      " [-1.8         1.5        -0.4        -5.5       ]]\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:25:30.229041Z",
     "start_time": "2025-03-29T10:25:30.103467Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.datasets import load_iris, fetch_20newsgroups, fetch_california_housing\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "from sklearn.naive_bayes import MultinomialNB\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.feature_extraction import DictVectorizer\n",
    "from sklearn.tree import DecisionTreeClassifier, export_graphviz\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.metrics import roc_auc_score\n",
    "import time"
   ],
   "id": "5ef4d4bdc82bc193",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:25:30.242571Z",
     "start_time": "2025-03-29T10:25:30.239577Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#鸢尾花数据集，查看特征，目标，样本量\n",
    "\n",
    "# li = load_iris()\n",
    "#\n",
    "# print(\"获取特征值\")\n",
    "# print(type(li.data))\n",
    "# print(\"-\" * 50)\n",
    "# print(li.data.shape)\n",
    "# li.data[:5]"
   ],
   "id": "db80fef15a9ac0ca",
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:25:30.285005Z",
     "start_time": "2025-03-29T10:25:30.281903Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 下面是比较大的数据，需要下载一会儿，20类新闻\n",
    "# subset='all'下载全部数据，subset='train'下载训练集，subset='test'下载测试集\n",
    "# data_home=\"data\"指定下载路径\n",
    "# news = fetch_20newsgroups(subset='all', data_home=\"data\")\n",
    "# print(\"第一个文本\")\n",
    "# print(news.data[0])\n",
    "# print(type(news.data))\n",
    "# len(news.target_names)"
   ],
   "id": "ab4adc575b047249",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:25:30.313006Z",
     "start_time": "2025-03-29T10:25:30.310009Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# print(news.target[0:30])\n",
    "# from pprint import pprint\n",
    "#\n",
    "# pprint(list(news.target_names))"
   ],
   "id": "980d37b5fa93f267",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:25:30.334849Z",
     "start_time": "2025-03-29T10:25:30.331647Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# house = fetch_california_housing(data_home=\"data\")\n",
    "# print(\"获取特征值\")\n",
    "# print(house.data[0])\n",
    "# print(\"样本的形状\")\n",
    "# print(house.data.shape)\n",
    "# print(\"-\" * 50)"
   ],
   "id": "c0731eb65f65e409",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:25:30.361832Z",
     "start_time": "2025-03-29T10:25:30.358853Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# print(\"目标值\")\n",
    "# print(house.target[0:10])\n",
    "# print(\"-\" * 50)\n",
    "# print(house.DESCR)\n",
    "# print(\"-\" * 50)\n",
    "# print(house.feature_names)\n",
    "# print(\"-\" * 50)\n",
    "# house.target.shape"
   ],
   "id": "6fad7038e8b6d8c6",
   "outputs": [],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:25:40.540846Z",
     "start_time": "2025-03-29T10:25:30.383442Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 读取数据\n",
    "data = pd.read_csv(\"./data/kaggle_data/train.csv\")\n",
    "data.head()"
   ],
   "id": "4676139ff44ce62d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   row_id       x       y  accuracy    time    place_id\n",
       "0       0  0.7941  9.0809        54  470702  8523065625\n",
       "1       1  5.9567  4.7968        13  186555  1757726713\n",
       "2       2  8.3078  7.0407        74  322648  1137537235\n",
       "3       3  7.3665  2.5165        65  704587  6567393236\n",
       "4       4  4.0961  1.1307        31  472130  7440663949"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.7941</td>\n",
       "      <td>9.0809</td>\n",
       "      <td>54</td>\n",
       "      <td>470702</td>\n",
       "      <td>8523065625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>5.9567</td>\n",
       "      <td>4.7968</td>\n",
       "      <td>13</td>\n",
       "      <td>186555</td>\n",
       "      <td>1757726713</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>8.3078</td>\n",
       "      <td>7.0407</td>\n",
       "      <td>74</td>\n",
       "      <td>322648</td>\n",
       "      <td>1137537235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>7.3665</td>\n",
       "      <td>2.5165</td>\n",
       "      <td>65</td>\n",
       "      <td>704587</td>\n",
       "      <td>6567393236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>4.0961</td>\n",
       "      <td>1.1307</td>\n",
       "      <td>31</td>\n",
       "      <td>472130</td>\n",
       "      <td>7440663949</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:25:41.051527Z",
     "start_time": "2025-03-29T10:25:40.593753Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 处理数据\n",
    "# 1.缩小数据\n",
    "data = data.query('x>1.0 & x<1.25 & y>2.5 & y<2.75')\n",
    "# 2.转换时间戳为年月日格式\n",
    "time_value = pd.to_datetime(data['time'], unit='s')\n",
    "# 把日期格式转换为字典数据\n",
    "time_value = pd.DatetimeIndex(time_value)\n",
    "# 3.构造特征\n",
    "# data['day'] = time_value.day\n",
    "# data['hour'] = time_value.hour\n",
    "# data['weekday'] = time_value.weekday\n",
    "data.head()\n"
   ],
   "id": "324d204b2b9d8eed",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      row_id       x       y  accuracy    time    place_id\n",
       "600      600  1.2214  2.7023        17   65380  6683426742\n",
       "957      957  1.1832  2.6891        58  785470  6683426742\n",
       "4345    4345  1.1935  2.6550        11  400082  6889790653\n",
       "4735    4735  1.1452  2.6074        49  514983  6822359752\n",
       "5580    5580  1.0089  2.7287        19  732410  1527921905"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>600</th>\n",
       "      <td>600</td>\n",
       "      <td>1.2214</td>\n",
       "      <td>2.7023</td>\n",
       "      <td>17</td>\n",
       "      <td>65380</td>\n",
       "      <td>6683426742</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>957</th>\n",
       "      <td>957</td>\n",
       "      <td>1.1832</td>\n",
       "      <td>2.6891</td>\n",
       "      <td>58</td>\n",
       "      <td>785470</td>\n",
       "      <td>6683426742</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4345</th>\n",
       "      <td>4345</td>\n",
       "      <td>1.1935</td>\n",
       "      <td>2.6550</td>\n",
       "      <td>11</td>\n",
       "      <td>400082</td>\n",
       "      <td>6889790653</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4735</th>\n",
       "      <td>4735</td>\n",
       "      <td>1.1452</td>\n",
       "      <td>2.6074</td>\n",
       "      <td>49</td>\n",
       "      <td>514983</td>\n",
       "      <td>6822359752</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5580</th>\n",
       "      <td>5580</td>\n",
       "      <td>1.0089</td>\n",
       "      <td>2.7287</td>\n",
       "      <td>19</td>\n",
       "      <td>732410</td>\n",
       "      <td>1527921905</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:25:41.125580Z",
     "start_time": "2025-03-29T10:25:41.117580Z"
    }
   },
   "cell_type": "code",
   "source": [
    "time_value = pd.DatetimeIndex(time_value)\n",
    "\n",
    "print(\"-\" * 50)\n",
    "print(time_value[0:10])"
   ],
   "id": "1cca1d678037e60a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "DatetimeIndex(['1970-01-01 18:09:40', '1970-01-10 02:11:10',\n",
      "               '1970-01-05 15:08:02', '1970-01-06 23:03:03',\n",
      "               '1970-01-09 11:26:50', '1970-01-02 16:25:07',\n",
      "               '1970-01-04 15:52:57', '1970-01-01 10:13:36',\n",
      "               '1970-01-09 15:26:06', '1970-01-08 23:52:02'],\n",
      "              dtype='datetime64[ns]', name='time', freq=None)\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:25:41.268617Z",
     "start_time": "2025-03-29T10:25:41.263013Z"
    }
   },
   "cell_type": "code",
   "source": "data.shape",
   "id": "b2dc1331a68029b6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(17710, 6)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:25:41.427123Z",
     "start_time": "2025-03-29T10:25:41.423835Z"
    }
   },
   "cell_type": "code",
   "source": "print(type(data))",
   "id": "3ac400ed7db872d3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:25:41.508630Z",
     "start_time": "2025-03-29T10:25:41.496629Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data.insert(data.shape[1], \"day\", time_value.day)\n",
    "data.insert(data.shape[1], \"hour\", time_value.hour)\n",
    "data.insert(data.shape[1], \"weekday\", time_value.weekday)\n",
    "data.head()"
   ],
   "id": "f093da4306468cf7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      row_id       x       y  accuracy    time    place_id  day  hour  weekday\n",
       "600      600  1.2214  2.7023        17   65380  6683426742    1    18        3\n",
       "957      957  1.1832  2.6891        58  785470  6683426742   10     2        5\n",
       "4345    4345  1.1935  2.6550        11  400082  6889790653    5    15        0\n",
       "4735    4735  1.1452  2.6074        49  514983  6822359752    6    23        1\n",
       "5580    5580  1.0089  2.7287        19  732410  1527921905    9    11        4"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "      <th>day</th>\n",
       "      <th>hour</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>600</th>\n",
       "      <td>600</td>\n",
       "      <td>1.2214</td>\n",
       "      <td>2.7023</td>\n",
       "      <td>17</td>\n",
       "      <td>65380</td>\n",
       "      <td>6683426742</td>\n",
       "      <td>1</td>\n",
       "      <td>18</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>957</th>\n",
       "      <td>957</td>\n",
       "      <td>1.1832</td>\n",
       "      <td>2.6891</td>\n",
       "      <td>58</td>\n",
       "      <td>785470</td>\n",
       "      <td>6683426742</td>\n",
       "      <td>10</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4345</th>\n",
       "      <td>4345</td>\n",
       "      <td>1.1935</td>\n",
       "      <td>2.6550</td>\n",
       "      <td>11</td>\n",
       "      <td>400082</td>\n",
       "      <td>6889790653</td>\n",
       "      <td>5</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4735</th>\n",
       "      <td>4735</td>\n",
       "      <td>1.1452</td>\n",
       "      <td>2.6074</td>\n",
       "      <td>49</td>\n",
       "      <td>514983</td>\n",
       "      <td>6822359752</td>\n",
       "      <td>6</td>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5580</th>\n",
       "      <td>5580</td>\n",
       "      <td>1.0089</td>\n",
       "      <td>2.7287</td>\n",
       "      <td>19</td>\n",
       "      <td>732410</td>\n",
       "      <td>1527921905</td>\n",
       "      <td>9</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:34:36.783859Z",
     "start_time": "2025-03-29T10:34:36.775624Z"
    }
   },
   "cell_type": "code",
   "source": "data = data.drop(\"time\", axis=1)",
   "id": "d78fbf75ffb7eb1d",
   "outputs": [],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:41:13.190090Z",
     "start_time": "2025-03-29T10:41:13.178088Z"
    }
   },
   "cell_type": "code",
   "source": [
    "per = pd.Period(\"2025-3-29 18:00:00\", \"h\")\n",
    "per.weekday"
   ],
   "id": "4c231dfc523bac26",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:41:15.197003Z",
     "start_time": "2025-03-29T10:41:15.165698Z"
    }
   },
   "cell_type": "code",
   "source": "data.describe()",
   "id": "ef43e4a08939bf5b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             row_id             x             y      accuracy      place_id  \\\n",
       "count  1.691800e+04  16918.000000  16918.000000  16918.000000  1.691800e+04   \n",
       "mean   1.451472e+07      1.122553      2.632834     81.444320  5.118619e+09   \n",
       "std    8.354976e+06      0.077291      0.069877    109.536306  2.347962e+09   \n",
       "min    6.000000e+02      1.000100      2.500100      1.000000  1.097201e+09   \n",
       "25%    7.324091e+06      1.048800      2.574800     26.000000  3.312464e+09   \n",
       "50%    1.444374e+07      1.123400      2.643350     63.000000  5.261906e+09   \n",
       "75%    2.164847e+07      1.190600      2.687900     75.000000  6.766325e+09   \n",
       "max    2.911215e+07      1.249900      2.749900   1000.000000  9.980711e+09   \n",
       "\n",
       "                day          hour       weekday  \n",
       "count  16918.000000  16918.000000  16918.000000  \n",
       "mean       5.083993     11.470327      3.101962  \n",
       "std        2.716574      6.942576      1.670058  \n",
       "min        1.000000      0.000000      0.000000  \n",
       "25%        2.000000      5.000000      2.000000  \n",
       "50%        5.000000     12.000000      3.000000  \n",
       "75%        7.000000     17.000000      4.000000  \n",
       "max       10.000000     23.000000      6.000000  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>place_id</th>\n",
       "      <th>day</th>\n",
       "      <th>hour</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1.691800e+04</td>\n",
       "      <td>16918.000000</td>\n",
       "      <td>16918.000000</td>\n",
       "      <td>16918.000000</td>\n",
       "      <td>1.691800e+04</td>\n",
       "      <td>16918.000000</td>\n",
       "      <td>16918.000000</td>\n",
       "      <td>16918.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.451472e+07</td>\n",
       "      <td>1.122553</td>\n",
       "      <td>2.632834</td>\n",
       "      <td>81.444320</td>\n",
       "      <td>5.118619e+09</td>\n",
       "      <td>5.083993</td>\n",
       "      <td>11.470327</td>\n",
       "      <td>3.101962</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>8.354976e+06</td>\n",
       "      <td>0.077291</td>\n",
       "      <td>0.069877</td>\n",
       "      <td>109.536306</td>\n",
       "      <td>2.347962e+09</td>\n",
       "      <td>2.716574</td>\n",
       "      <td>6.942576</td>\n",
       "      <td>1.670058</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>6.000000e+02</td>\n",
       "      <td>1.000100</td>\n",
       "      <td>2.500100</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.097201e+09</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>7.324091e+06</td>\n",
       "      <td>1.048800</td>\n",
       "      <td>2.574800</td>\n",
       "      <td>26.000000</td>\n",
       "      <td>3.312464e+09</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.444374e+07</td>\n",
       "      <td>1.123400</td>\n",
       "      <td>2.643350</td>\n",
       "      <td>63.000000</td>\n",
       "      <td>5.261906e+09</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>12.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.164847e+07</td>\n",
       "      <td>1.190600</td>\n",
       "      <td>2.687900</td>\n",
       "      <td>75.000000</td>\n",
       "      <td>6.766325e+09</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>17.000000</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2.911215e+07</td>\n",
       "      <td>1.249900</td>\n",
       "      <td>2.749900</td>\n",
       "      <td>1000.000000</td>\n",
       "      <td>9.980711e+09</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>23.000000</td>\n",
       "      <td>6.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:41:18.020403Z",
     "start_time": "2025-03-29T10:41:18.008897Z"
    }
   },
   "cell_type": "code",
   "source": [
    "place_count = data.groupby(\"place_id\").count()\n",
    "place_count"
   ],
   "id": "2e36138b96f7e7b9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "            row_id     x     y  accuracy   day  hour  weekday\n",
       "place_id                                                     \n",
       "1097200869    1044  1044  1044      1044  1044  1044     1044\n",
       "1228935308     120   120   120       120   120   120      120\n",
       "1267801529      58    58    58        58    58    58       58\n",
       "1278040507      15    15    15        15    15    15       15\n",
       "1285051622      21    21    21        21    21    21       21\n",
       "...            ...   ...   ...       ...   ...   ...      ...\n",
       "9741307878       5     5     5         5     5     5        5\n",
       "9753855529      21    21    21        21    21    21       21\n",
       "9806043737       6     6     6         6     6     6        6\n",
       "9809476069      23    23    23        23    23    23       23\n",
       "9980711012       5     5     5         5     5     5        5\n",
       "\n",
       "[239 rows x 7 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>day</th>\n",
       "      <th>hour</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>place_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1097200869</th>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1228935308</th>\n",
       "      <td>120</td>\n",
       "      <td>120</td>\n",
       "      <td>120</td>\n",
       "      <td>120</td>\n",
       "      <td>120</td>\n",
       "      <td>120</td>\n",
       "      <td>120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1267801529</th>\n",
       "      <td>58</td>\n",
       "      <td>58</td>\n",
       "      <td>58</td>\n",
       "      <td>58</td>\n",
       "      <td>58</td>\n",
       "      <td>58</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1278040507</th>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1285051622</th>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9741307878</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9753855529</th>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9806043737</th>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9809476069</th>\n",
       "      <td>23</td>\n",
       "      <td>23</td>\n",
       "      <td>23</td>\n",
       "      <td>23</td>\n",
       "      <td>23</td>\n",
       "      <td>23</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9980711012</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>239 rows × 7 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:41:21.914833Z",
     "start_time": "2025-03-29T10:41:21.907579Z"
    }
   },
   "cell_type": "code",
   "source": "place_count[\"x\"].describe()",
   "id": "887e26fb2d2cd48e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count     239.000000\n",
       "mean       70.786611\n",
       "std       152.745630\n",
       "min         4.000000\n",
       "25%         6.000000\n",
       "50%        15.000000\n",
       "75%        59.500000\n",
       "max      1044.000000\n",
       "Name: x, dtype: float64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:41:25.903498Z",
     "start_time": "2025-03-29T10:41:25.898225Z"
    }
   },
   "cell_type": "code",
   "source": [
    "tf = place_count[place_count.x>3].reset_index()\n",
    "tf.shape"
   ],
   "id": "9fbba3785b90acdd",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(239, 8)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:41:35.226808Z",
     "start_time": "2025-03-29T10:41:35.221347Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = data[data[\"place_id\"].isin(tf.place_id)]\n",
    "data.shape"
   ],
   "id": "4eeadc9c27ec9ea1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(16918, 8)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T10:53:57.110063Z",
     "start_time": "2025-03-29T10:53:57.104064Z"
    }
   },
   "cell_type": "code",
   "source": [
    "y = data['place_id']\n",
    "x = data.drop(['place_id'], axis=1)\n",
    "x = x.drop(['row_id'], axis=1)\n",
    "print(x.shape)\n",
    "print(x.columns)"
   ],
   "id": "dd13e96c3fd56d1e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(16918, 6)\n",
      "Index(['x', 'y', 'accuracy', 'day', 'hour', 'weekday'], dtype='object')\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T11:50:42.549522Z",
     "start_time": "2025-03-29T11:50:42.526057Z"
    }
   },
   "cell_type": "code",
   "source": [
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=1)\n",
    "\n",
    "std = StandardScaler()\n",
    "\n",
    "x_train = std.fit_transform(x_train)\n",
    "x_test = std.transform(x_test)\n"
   ],
   "id": "e936773e0adf2c60",
   "outputs": [],
   "execution_count": 34
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# knn算法",
   "id": "1cd07585196b79b5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-29T11:55:25.658445Z",
     "start_time": "2025-03-29T11:55:25.396098Z"
    }
   },
   "cell_type": "code",
   "source": [
    "knn = KNeighborsClassifier(n_neighbors=5)\n",
    "\n",
    "knn.fit(x_train, y_train)\n",
    "\n",
    "y_predict = knn.predict(x_test)\n",
    "\n",
    "print(\"预测的目标签到位置为：\", y_predict[0:10])\n",
    "\n",
    "print(\"预测的准确率\", knn.score(x_test, y_test))"
   ],
   "id": "c79c4d8943351bee",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测的目标签到位置为： [5689129232 1097200869 2355236719 9632980559 6424972551 4022692381\n",
      " 8048985799 3533177779 1435128522 3312463746]\n",
      "预测的准确率 0.4806146572104019\n"
     ]
    }
   ],
   "execution_count": 38
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
